#pragma once
#include "cybertron/bridge/CybertronBridgeBase.hpp"
#include "cybertron/network/CybertronSyncConn.hpp"
#include "cybertron/core/UtilSharedMemory.hpp"
#include "cybertron/core/UtilPath.hpp"
#include "cybertron/core/UtilProcess.hpp"
#include "cybertron/sensor/noise/IMUNoiseMaker.hpp"
#include "cybertron/core/Log.hpp"
#include "SimOneIOStruct.h"
#include <vector>
#include <map>
#include <string>
#include "Net/BridgeIOServer.hpp"
#include  "Matlab/TaskSimulinkRun.hpp"
class SimulinkBase;

CYBERTRON_BEGIN

class TaskSMScenarioEvent;
class CybertronBridgeIO : public CybertronBridgeBase
{
public:
	//CybertronBridgeIO(std::shared_ptr<BridgeIOServer> pServer);
	CybertronBridgeIO();
	virtual ~CybertronBridgeIO();
	virtual bool init(int argc, char* argv[]);
	NodeHotAreaManager*  GetHotAreaManager() {
		return &mHotAreaManager;
	}
	std::shared_ptr<BridgeIOServer> getServer() { return mpBridgeIOServer; }

	void SendHotareaDataSuberMainVehicle(int mainVehicleId, Message* pMessage) {
		mpBridgeIOServer->SendHotareaDataSuberMainVehicle(mainVehicleId, pMessage);
	}
	void SendOutSystemNode(Message* pMessage) {
		mpBridgeIOServer->BroadcastOutSystemNode(pMessage);
	}
	void SendMainVehicleChangeReady(int mainVehicleId) {
		mpBridgeIOServer->SendMainVehicleStatus(mainVehicleId, 0);
	}
protected:
	void getVerion();
	virtual bool onTaskStartAfter() override;
	//virtual bool onTaskRunning() override;
	//virtual bool onTaskStop() override;
	virtual bool onLoadConfig() override;
	virtual bool onStartBridge() override;
	void runServer();
	///virtual bool onStartBridge() override;
	virtual void onMessageCommand(std::uint32_t msgId, const std::string* msgDataBody) override;

	virtual void onTimer(int framestamp, int ms, int msModNs) override;
	virtual void onCaseStart() override;
	virtual void onCaseStop() override;
	virtual void onPlay() override;
	virtual void onPause() override;
	virtual void onReset() override;
	virtual void onEnd() override; // onEnd will be called when FromAnyRequestTaskEnd received
	virtual bool onTaskStop() override;
protected:
	struct gpsItem
	{
		vec3 Pos;
		vec3 Rot;
		vec3 Vel;
		float Throttle;
		float Brake;
		float Steering;
		int Gear;
		vec3 Accel;
		vec3 AngVel;
		float WheelSpeedFL;
		float WheelSpeedFR;
		float WheelSpeedRL;
		float WheelSpeedRR;
	};

private:
    //void InitMsgMap();
    //std::map<size_t, std::string> mMsgMap;
    std::vector<std::string> mRegisteredMsg;
	std::string mStartScriptPath;
	std::string mEndScriptPath;

#ifdef CYBERTRON_LINUX
	cybertron::ProcessHandle mStartProcessHandle;
	cybertron::ProcessHandle mEndProcessHandle;
	void startScriptWithSsh(const std::string& script);
#endif
	void SetHDMapInfo(std::string OD, std::string ODUrl, std::string ODMd5);
	std::shared_ptr<TaskSMScenarioEvent> mScenarioEventTask;

	int mFrame;
	bool mbGetHotArea;
	int mVehicleId;
	std::string mVehicleIP;
	int mVehiclePort;
	IMUNoiseMakerPtr mIMUNoiseMaker;
	ProcessHandle mStartProcess;
	std::shared_ptr<BridgeIOServer>	mpBridgeIOServer;
	std::shared_ptr<TaskControlModuleRun> mpTaskControlModuleRun;

	bool mbRunning;
    bool mMatlabEnable = false;
	bool mIsCanOnTimering;
#ifdef CYBERTRON_WIN
    SimulinkBase* mSimulinkInterface = nullptr;
#endif
};

CYBERTRON_END